import Vue from 'vue'
import App from './App.vue'
// 图标
import 'font-awesome/css/font-awesome.min.css'
// 全局使用axios
import axios from 'axios'
import router from './router'
import {getToken} from './utils/setToken'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts

Vue.config.productionTip = false
Vue.prototype.axios = axios//挂在到原型可以在全局使用




// 配置axios基础地
axios.defaults.baseURL =''//后端接口域名

// 请求拦截器
axios.interceptors.request.use(
config => {
const token =getToken('token')
if(token) {
  //后端要求的格式 Authorization: Bearer ${token}
config.headers.Authorization = `Bearer ${token}`; 
}
return config
},
error => {
  return Promise.reject(error)
}
)


// 响应拦截器
axios.interceptors.response.use(
  response => {
    return response//返回响应数据
  },
  error => {
    //处理401
    if(error.response && error.response.status ===401) {
      // 清除无效的token
      localStorage.removeItem('token')
      router.push('/login')
      Vue.prototype.$message.error('登录过期请重新登录')
    }
    return Promise.reject(error)
  }
)


new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
